package com.example.trb.service.user;

import com.example.trb.component.EncodeComponent;
import com.example.trb.dto.UserDTO;
import com.example.trb.entity.User;
import com.example.trb.entity.UserInfo;
import com.example.trb.mapper.UserInfoMapper;
import com.example.trb.mapper.UserMapper;
import com.example.trb.util.result.UserReturnCode;
import com.example.trb.vo.UpdatePasswordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    EncodeComponent encoder;
    @Value("${user_default_name}")
    String userDefaultName;

    public Integer getUserInfoId(Integer userId) {
        return userInfoMapper.getId(userId);
    }

    public UserDTO setPassword(UpdatePasswordVO vo) {
        UserDTO dto = new UserDTO();
        User result = userMapper.selectById(vo.getUserId());

        if (result == null) {
            dto.setCode(UserReturnCode.USER_NOT_EXIST);
            return dto;
        }
        if (encoder.matches(vo.getOriginPassword(), result.getPassword())) {
            result.setPassword(encoder.encode(vo.getNewPassword()));
            userMapper.updateById(result);
            dto.setCode(UserReturnCode.OK);
        } else {
            dto.setCode(UserReturnCode.PASSWORD_ERROR);
        }
        return dto;
    }


    public UserDTO setUserInfo(UserInfo userInfo) {
        UserDTO dto = new UserDTO();
        userInfoMapper.updateById(userInfo);
        dto.setCode(UserReturnCode.OK);
        return dto;
    }

    public UserDTO addUserInfo(UserInfo userInfo) {
        UserDTO dto = new UserDTO();
        int id = userInfoMapper.insert(userInfo);
        dto.setCode(UserReturnCode.OK);
        dto.setData(id);
        return dto;
    }

    public UserDTO getUserAllInfo(Integer userId) {
        Map<String, Object> queryMap = new HashMap<>(1);
        queryMap.put("user_id", userId);

        UserDTO dto = new UserDTO();
        List<UserInfo> results = userInfoMapper.selectByMap(queryMap);
        if (results.isEmpty()) {
            dto.setCode(UserReturnCode.USER_NOT_EXIST);
            return dto;
        }

        dto.setCode(UserReturnCode.OK);
        dto.setData(results.get(0));
        return dto;
    }

    public UserDTO register(User user) {
        UserDTO dto = new UserDTO();
        User extract = new User();
        extract.setAccount(user.getAccount());
        extract.setPassword(user.getPassword());

        User exist = userMapper.findSameAccount(extract.getAccount());
        if (exist != null) {
            dto.setCode(UserReturnCode.USER_EXIST);
            return dto;
        }

        String encodePassword = encoder.encode(extract.getPassword());
        extract.setPassword(encodePassword);
        extract.setName(userDefaultName);
        userMapper.insert(extract);
        extract.setPassword(null);

        dto.setData(extract);
        dto.setCode(UserReturnCode.OK);
        return dto;
    }

    public UserDTO login(User user) {
        UserDTO dto = new UserDTO();
        User result = userMapper.selectByAccount(user.getAccount());

        if (result == null) {
            dto.setCode(UserReturnCode.USER_NOT_EXIST);
            return dto;
        }
        if (encoder.matches(user.getPassword(), result.getPassword())) {
            result.setPassword(null);
            dto.setCode(UserReturnCode.OK);
            dto.setData(result);
            return dto;
        }

        dto.setCode(UserReturnCode.PASSWORD_OR_ACCOUNT_ERROR);
        return dto;
    }
}
